package com.zhang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.entity.CartInfo;
import com.zhang.mapper.CartInfoMapper;
import com.zhang.service.AsyncCartInfoService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Async
@Service
public class AsyncCartInfoServiceImpl extends ServiceImpl<CartInfoMapper, CartInfo> implements AsyncCartInfoService {
    @Override
    public void updateCartInfo(CartInfo existCartInfo) {
        baseMapper.updateById(existCartInfo);
    }

    @Override
    public void saveCartInfo(CartInfo existCartInfo) {
        baseMapper.insert(existCartInfo);
    }

    //删除数据库里面的内容
    @Override
    public void deleteDbCart(String oneOfUserId, Long skuId) {
        QueryWrapper<CartInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",oneOfUserId);
        wrapper.eq("sku_id",skuId);
        baseMapper.delete(wrapper);
    }

    //从数据库中查询记录修改
    @Override
    public void checkDbCart(String oneOfUserId, Long skuId, Integer isChecked) {
        //update cart_info set is_checked=1 where user_id=xxx and sku_id=xxx
        CartInfo cartInfo = new CartInfo();
        cartInfo.setIsChecked(isChecked);
        QueryWrapper<CartInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",oneOfUserId);
        wrapper.eq("sku_id",skuId);
        baseMapper.update(cartInfo,wrapper);
    }
}
